package com.dealin.writer.bean;

import com.google.gson.Gson;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.Date;

/**
 * Created by lenovo on 2016/6/20.
 */
public class Group {
    private String name="未命名",createDate=new Date().toLocaleString(),refreshDate=new Date().toLocaleString(),summary="暂无摘要",password="";
    private int chapterCount=0,id=-1;
    public boolean isExpanded=false;
    private ArrayList<Chapter> chapters= new ArrayList<Chapter>();
    private JSONObject groupJSONObject;
    public Group()
    {

    }
    public Group(JSONObject jo) throws Exception
    {
        init(jo);
    }
    public void createNewChapter(String name)
    {
        Chapter ch=new Chapter();
        ch.setName(name);
        ch.setContent("　　");
        ch.setCreateDate(new Date().toLocaleString());
        ch.setRefreshDate(ch.getCreateDate());
        ch.setSummary("暂无摘要");
        ch.setPassword("无");
        ch.setId(chapterCount);
        chapterCount+=1;
        chapters.add(ch);
    }
    //初始化
    public void init(JSONObject project) throws Exception
    {
        name=project.getString("name");
        createDate=project.getString("createDate");
        refreshDate=project.getString("refreshDate");
        password=project.getString("password");
        summary=project.getString("summary");
        chapterCount=project.getInt("chapterCount");
        id = project.getInt("id");
        isExpanded = project.getBoolean("isExpanded");
        JSONArray ja=project.getJSONArray("chapters");
        for(int i=0;i<ja.length();i++)
        {
            chapters.add(new Chapter( ja.getJSONObject(i)));
        }
    }
    //转换为JSONObject
    public String toJSONObject() throws Exception
    {
        /*JSONObject project=new JSONObject();
        project.put("name",name);
        project.put("createDate",createDate);
        project.put("refreshDate",refreshDate);
        project.put("password",password);
        project.put("summary",summary);
        project.put("chapterCount",chapters.size());*/
        return new Gson().toJson(this);
    }

    public Chapter getChapter(int index)
    {
        if(index>=0&&index<chapters.size())
        {
            return chapters.get(index);
        }else{
            return null;
        }
    }
    public boolean deleteChapter(int chapterIndex)
    {
        if(chapterIndex>=0&&chapterIndex<chapters.size()) {
            chapters.remove(chapterIndex);
            chapterCount=chapters.size();
            return true;
        }else{
            return false;
        }
    }
    public String getName()
    {
        return name;
    }
    public String getCreateDate()
    {
        return createDate;
    }
    public String getRefreshDate()
    {
        return refreshDate;
    }
    public String getSummary()
    {
        return summary;
    }
    public String getPassword()
    {
        return password;
    }
    public int getChapterCount()
    {
        return chapterCount;
    }

    public int getId() {
        return id;
    }
    public int getCount(){
        int count=0;
        for(int i=0;i<chapterCount;i++){
            count+=chapters.get(i).getCount();
        }
        return count;
    }
    /*
    **以下是set方法
     */
    public void setName(String s)
    {
        name=s;
    }
    public void setCreateDate(String s)
    {
        createDate=s;
    }
    public void setRefreshDate(String s)
    {
        refreshDate=s;
    }
    public void setSummary(String s)
    {
        summary=s;
    }
    public void setPassword(String s)
    {
        password=s;
    }

    public void setId(int id) {
        this.id=id;
    }

}
